import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
          useMaterial3: false,
          primarySwatch: Colors.blue,
        ),
        home: const MyHome()
    );
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> with SingleTickerProviderStateMixin {

  late AnimationController _animationController;

  bool flag = true;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1000)
    );
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: const Text("Flutter App"),
      ),
      body: Center(
        child: Stack(
          children: [
           ScaleTransition(
             //前面0到0.5秒,icon从1到0
             scale: _animationController.drive(
               Tween(begin: 1.0,end: 0.0).chain(
                 CurveTween(curve: const Interval(0,0.5))
               )
             ),
             child: const  Icon(Icons.close, size: 40,)
           ),
           ScaleTransition(
             //后面面0.5到1秒,icon从0到1
             scale: _animationController.drive(
               Tween(begin: 0.0,end: 1.0).chain(
                  CurveTween(curve: const Interval(0.5, 1)))
             ),
             child: const Icon(Icons.search, size: 40,),
           ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          flag =!flag;
          flag ? _animationController.forward() : _animationController.reverse();
        },
        child: const Icon(Icons.refresh),
      ),
    );
  }
}
